Promise.myAll = function(array) {
    const result = []
    let index = 0
    
    return new Promise((resolve, reject) => {
        function addData(key, value) {
            result[key] = value
            index++
            if (index === array.length) {
                resolve(result)
            }
        }
        for (let i = 0; i < array.length; i++) {
            const current = array[i]
            if (current instanceof Promise) {
                current.then(
                    value => addData(i, value),
                    reason => reject(reason)
                )
            } else {
                addData(i, current)
            }
        }
    })
}


function p1 () {
    return new Promise(function(resolve, reject) {
        setTimeout(function() {
            resolve('p1');
        }, 2000)
    })
}
 
function p2 () {
    return new Promise(function(resolve, reject) {
        resolve('p2 success');
        // reject('p2 reject')
    })
}

// Promise.myAll(['a', 'b', p1(), p2(), 'c'])
//    .then(value => console.log(value))

function unique(arr, key) {
    const res = new Map()
    return arr.filter(item => {
        if (key) {
            // 根据对象指定字段去重
            return !res.has(item[key]) && res.set(item[key], 1)
        } else {
            return !res.has(item) && res.set(item, 1)
        }
    });
}
console.log(unique([1, 2, 3, 2, 'NaN', NaN, 0, 0]))
console.log(unique([{name: '1', b: 2}, {name: '2', c: 3}, {name: '1', b: 2}, {name: '2', c: 3}], 'name'))